// 搜索
/**
 * 点击搜索按钮发送请求 获取input的值作为word数据传输
 */
function search() {

    console.log($('#goodSelect').height())

    console.log('ddd');
    $.ajax({
        type: 'get',
        url: 'http://www.xiongmaoyouxuan.com/api/search',
        data: {
            word: $('#search').val()
        },
        success: function (res) {

            console.log(res)
            let slist = res.data.list
            let h = 0
            dealProductList(slist)
            if (slist.length % 4 != 0) {
                slist.splice(slist.length - slist.length % 4 - 1, slist.length % 4)
            }
            let searchList = template('search-list', {
                productListData: slist
            })
            $('#home').html(searchList)
        }

    })

}

/**
 * 首页产品展示
 */
let productList = []

function indexPageProducts() {
    $.ajax({
        type: 'get',
        url: 'http://www.xiongmaoyouxuan.com/api/tab/1?start=0',
        success: function (res) {
            // console.log(res.data.items.list)
            //为了方便渲染，修改一下数据
            let list = res.data.items.list
            dealProductList(list)

            // console.log(list)
            if (list.length % 4 != 0) {
                list.splice(list.length - list.length % 4 - 1, list.length % 4)
            }
            productList = list

            let ProductList = template('product-list', {
                productListData: list
            })
            // goodSelectHeight(list.length)
            $('.productsShow ul').html(ProductList)
            // toDetail()
        }
    })
}

/**
 * 商品列表数据处理
 */
function dealProductList(products) {
    products.map(function (item) {
        item.couponValue = parseInt(item.couponValue)
        item.float = parseInt((item.price - parseInt(item.price)) * 10)
        item.price = parseInt(item.price)
    })
}

/**
 * 首页菜单 -轮播图 -小模块
 */
function indexPageMenu() {
    $.ajax({
        type: 'get',
        url: 'http://www.xiongmaoyouxuan.com/api/tab/1?start=0',
        Headers: {
            'x-platform': 'pc'
        },
        success: function (res) {
            // console.log(res)
            //轮播
            let bn = [...res.data.banners]
            let bannerImg = ''
            if (bn.length != 0) {
                bn.forEach(item => {
                    bannerImg += `
                <div class="swiper-slide"><img src="${item.imageUrl}" alt=""></div>
                `
                })
                $('.swiper-wrapper').html(bannerImg)
            }

            //小模块：九块九包邮
            let blockData = res.data.gridsV2
            // console.log(blockData)
            let nineShip = `
                <h4>${blockData[0].title}</h4>
                <div class="noLoss">${blockData[0].text}</div>
                <img src="${blockData[0].imageUrl}" alt="">
            `
            $('.ninePacks').html(nineShip)

        }
    })
}

/**
 * 查看更多
 */
//页数
let pageNo = 1

function seeMoreProduct() {
    pageNo++
    $.ajax({
        type: 'get',
        url: 'http://www.xiongmaoyouxuan.com/api/tab/1/feeds',
        data: {
            start: pageNo * 20
        },
        success: function (res) {
            console.log(res.data.list)
            let datalist = res.data.list
            dealProductList(datalist)
            productList = [...productList, ...datalist]
            let plen = productList.length
            if (plen % 4 != 0) {
                productList.splice(productList.length - productList.length % 4 - 1, productList.length % 4)
            }

            let ProductList = template('product-list', {
                productListData: productList
            })
            $('.productsShow ul').html(ProductList)
        }
    })
}

/**
 * 点击商品进入详情页面
 */
function toDetail(id) {
    location.href = `productDetail.html?id=${id}`

}


/**
 * 菜单选项
 */
$('.menu').on('mouseover', function () {
    $('.rightBox')[0].style.display = 'block'
})




$('.menu ol').on('mouseover', 'li:nth-child(1)', function () {

    let hf = $(this).children()[1].innerHTML
    let hs = $(this).children()[2].innerHTML.substr(1)
    menuRequstOne('http://www.xiongmaoyouxuan.com/api/tab/2', 'http://www.xiongmaoyouxuan.com/api/tab/13')
    showMenuOne(hf)
    showMenuTwo(hs)
})

$('.menu ol').on('mouseover', 'li:nth-child(2)', function () {
    let hf = $(this).children()[1].innerHTML
    let hs = $(this).children()[2].innerHTML.substr(1)
    menuRequstOne('http://www.xiongmaoyouxuan.com/api/tab/5', 'http://www.xiongmaoyouxuan.com/api/tab/19')
    showMenuOne(hf)
    showMenuTwo(hs)
})

$('.menu ol').on('mouseover', 'li:nth-child(3)', function () {
    let hf = $(this).children()[1].innerHTML
    let hs = $(this).children()[2].innerHTML.substr(1) 
    menuRequstOne('http://www.xiongmaoyouxuan.com/api/tab/3', 'http://www.xiongmaoyouxuan.com/api/tab/15')
    showMenuOne(hf)
    showMenuTwo(hs)
})
$('.menu ol').on('mouseover', 'li:nth-child(4)', function () {
    let hf = $(this).children()[1].innerHTML
    let hs = $(this).children()[2].innerHTML.substr(1) 
    menuRequstOne('http://www.xiongmaoyouxuan.com/api/tab/4', 'http://www.xiongmaoyouxuan.com/api/tab/14')
    showMenuOne(hf)
    showMenuTwo(hs)
})
$('.menu ol').on('mouseover', 'li:nth-child(5)', function () {
    let hf = $(this).children()[1].innerHTML
    // let hs = $(this).children()[2].innerHTML.substr(1) 
    menuRequstOne('http://www.xiongmaoyouxuan.com/api/tab/10')
    showMenuOne(hf)
    // showMenuTwo(hs)
})
$('.menu ol').on('mouseover', 'li:nth-child(6)', function () {
    let hf = $(this).children()[1].innerHTML
    // let hs = $(this).children()[2].innerHTML.substr(1) 
    menuRequstOne('http://www.xiongmaoyouxuan.com/api/tab/11', 'http://www.xiongmaoyouxuan.com/api/tab/17')
    showMenuOne(hf)
    // showMenuTwo(hs)
})
$('.menu ol').on('mouseover', 'li:nth-child(7)', function () {
    let hf = $(this).children()[1].innerHTML
    let hs = $(this).children()[2].innerHTML.substr(1) 
    menuRequstOne('http://www.xiongmaoyouxuan.com/api/tab/6', 'http://www.xiongmaoyouxuan.com/api/tab/12')
    showMenuOne(hf)
    showMenuTwo(hs)
})
$('.menu ol').on('mouseover', 'li:nth-child(8)', function () {
    let hf = $(this).children()[1].innerHTML
    menuRequstOne('http://www.xiongmaoyouxuan.com/api/tab/16')
    showMenuOne(hf)
})
$('.menu ol').on('mouseover', 'li:nth-child(9)', function () {
    let hf = $(this).children()[1].innerHTML
    let hs = $(this).children()[2].innerHTML.substr(1) 
    menuRequstOne('http://www.xiongmaoyouxuan.com/api/tab/11', 'http://www.xiongmaoyouxuan.com/api/tab/7')
    showMenuOne(hf)
    showMenuTwo(hs)
})

$('.menu').on('mouseout', function () {
    $('.rightBox')[0].style.display = 'none'

})
$('.menu ol li').on('mouseout', function () {
    $('.rightBox').html('')

})

/**
 * 菜单发送请求
 */
function menuRequst(url, h) {
    $.ajax({
        type: 'get',
        url,
        success: function (res) {
            let data = res.data.categories
            let str = ''

            str += `
            <h4>${h}</h4>
            <ul class="clearfix">
            `
            data.forEach(function (item) {
                str += `               
                    <li>${item.word}</li>               
                `
            })
            str += `</ul></br>`
            let div = $('<div>').html(str)
            $('.rightBox').html(str)

        }
    })

}

let menuDataup
let menuDatado

function menuRequstOne(url1, url2) {
    $.ajax({
        type: 'get',
        url: url1,
        success: function (res) {
            let data = res.data.categories
            menuDataup = data
        }
    })
    $.ajax({
        type: 'get',
        url: url2,
        success: function (res) {
            let data = res.data.categories
            menuDatado = data
        }
    })
}

function showMenuOne(hf) {
    let str = `
            <h4>${hf}</h4>
            <ul class="clearfix">
            `
    menuDataup.forEach(function (item) {
        str += `<li>${item.word}</li>`
    })
    str += `</ul></br>`
    $('.rightBox').html(str)
    menuDataup=[]
}

function showMenuTwo(hs) {
    let str = `
    <h4>${hs}</h4>
    <ul class="clearfix">
    `
    menuDatado.forEach(function (item) {
        str += `<li>${item.word}</li>`
    })
    str += `</ul></br>`
    $('.rightBox').append(str)
    menuDatado = []
}

